var session = require('../../../utils/session.js');
var e = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    array:[],
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
  },

  bindGetUserInfo: function (res) {
    var that = this;
    if (res.detail.userInfo){
      that.wxLogin(res)
    } else {
      wx.showModal({
        title: "警告",
        content: "您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!",
        showCancel: !1,
        confirmText: "返回授权",
        success: function (res) {
          res.confirm;
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  wxLogin: function (authData) {
    var t = this;
    let detail = authData.detail
    let userInfo = detail.userInfo
    wx.getUserInfo({
      success: function (n) {
        wx.login({
          success: function (n) {
            wx.request({
              url: e.system.app_url_prefix + "/weapp/weixin/getWeiXinUserInfo?code=" + n.code,
              data: {},
              method: "GET",
              header: {
                Accept: "application/json",
                "Content-Type": "application/json; charset=utf-8"
              },
              success: function (res) {
                let code = parseInt(res.data.code)
                let data = res.data.data
                0 == code 
                  ? res && (
                  wx.setStorageSync("wechatOpenid", data.openId),
                  session.setUserInfo(userInfo),
                  session.setIsFirstLaunch(false), 
                    e.getUserDevice((car) => {
                      if (car) {
                        wx.reLaunch({
                          url: '/pages/home/index/index',
                        })
                      } else {
                        wx.redirectTo({
                          url: '/pages/login/index/index',
                        })
                      }
                    })
                ) 
                : wx.showModal({
                  title: "提示",
                  content: "微信授权失败",
                  showCancel: !1,
                  success: function (e) { }
                });
              },
              fail: function (e) {
                wx.showModal({
                  title: "提示",
                  content: "微信授权失败",
                  showCancel: !1,
                  success: function (e) { }
                });
              }
            });
          },
          fail: function () {
            console("登录获取Code失败！");
          }
        });
      }
    });
  },

  getBoundImeis: function (t) {
    var n = this;
    wx.request({
      url: e.system.app_url_prefix + "/rest/user2c/wechat/imeis",
      data: {
        wechatOpenid: t
      },
      method: "GET",
      header: {
        Accept: "application/json",
        "Content-Type": "application/json; charset=utf-8"
      },
      success: function (e) {
        200 == e.statusCode ? e.data && e.data instanceof Array && e.data.length > 0 ? wx.reLaunch({
          url: "../index/index?imei=" + e.data[0]
        }) : n.getUserCInfo(t) : wx.showModal({
          title: "提示",
          content: "获取设备列表失败",
          showCancel: !1,
          success: function (e) { }
        });
      },
      fail: function (e) {
        wx.showModal({
          title: "提示",
          content: "获取设备列表失败",
          showCancel: !1,
          success: function (e) { }
        });
      }
    });
  },

  getUserCInfo: function (t) {
    wx.request({
      url: e.system.app_url_prefix + "/rest/user2c/wechat",
      data: {
        wechatOpenid: t
      },
      method: "GET",
      header: {
        Accept: "application/json",
        "Content-Type": "application/json; charset=utf-8"
      },
      success: function (t) {
        // 200 == t.statusCode ? t.data ? 5 == e.system.source ? wx.redirectTo({
        //   url: "/pages/beian/beian"
        // }) : wx.redirectTo({
        //   url: "../bindImei/bindImei"
        // }) : wx.redirectTo({
        //   url: "../register/register"
        // }) : wx.showModal({
        //   title: "提示",
        //   content: "获取用户信息失败",
        //   showCancel: !1,
        //   success: function (e) { }
        // });
      },
      fail: function (e) {
        wx.showModal({
          title: "提示",
          content: "获取用户信息失败",
          showCancel: !1,
          success: function (e) { }
        });
      }
    });
  }

})